package cn.xy.service.impl;

import cn.xy.entity.User;
import cn.xy.mapper.UserMapper;
import cn.xy.service.UserService;
import cn.xy.service.ex.SelfRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Kiven
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        // 判断用户名和password不为空
        if(user.getUsername() == null || user.getPassword() == null){
            throw new SelfRuntimeException("请输入用户名或密码");
        }
        // 判断用户名是否存在
        User oldUser = userMapper.selectUserByUsername(user.getUsername());
        if(oldUser != null){
            throw new SelfRuntimeException("用户名已存在");
        }
        // 插入用户到数据库 需要准备数据
        Date date = new Date();
        user.setCreatedUser(user.getUsername());
        user.setUpdatedUser(user.getUsername());
        user.setCreatedTime(date);
        user.setUpdatedTime(date);
        userMapper.insertUser(user);

    }
}
